<template>
  <el-dialog
    title="评论"
    :modal="false"
    :width="isMobile ? '70%' : '30%'"
    top="40vh"
    center
    @close="handleClose"
    :visible.sync="dialogVisible"
  >
    <el-form
      ref="replyForm"
      :rules="rules"
      :model="replyForm"
      label-width="80px"
    >
      <el-form-item label="回复内容" prop="content">
        <el-input
          show-word-limit
          maxlength="1000"
          @keyup.enter.native="addReplyComment"
          v-model="replyForm.content"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addReplyComment">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    comment_id: {
      type: Number,
      required: true
    },
    reply_user_id: {
      type: Number,
      required: true
    },
    ShowDialog: {
      type: Boolean,
      required: true
    }
  },
  components: {},
  data() {
    return {
      dialogVisible: false,
      replyForm: {},
      rules: {
        content: [{ required: true, message: '评论不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    isMobile() {
      return this.$data.isMobile
    }
  },
  watch: {
    // 实时刷新
    ShowDialog: {
      immediate: true,
      handler(val, oldval) {
        this.dialogVisible = val
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    // 回复评论
    addReplyComment() {
      this.$refs.replyForm.validate(async valid => {
        if (!valid) this.$message.error('评论不能为空')
        // 数据格式化
        const { reply_user_id, comment_id } = this
        this.replyForm.reply_user_id = reply_user_id
        this.replyForm.comment_id = comment_id
        console.log('forms', this.replyForm)
        let datas = this.$qs.stringify(this.replyForm)
        const res = await this.$API.article.addReplyComment(datas)
        if (res.status == 200) {
          this.$message.success(res.msg)
          // 关闭对话框
          this.dialogVisible = false
          this.replyForm = {}
          // 重载评论列表
          this.$emit('custom-confirm')
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleClose() {
      this.dialogVisible = false
      this.$emit('custom-close')
    }
  }
}
</script>

<style scoped>
/* 移动端样式 */
@media screen and (max-width: 767px) {
  /* 其他移动端样式设置 */
}
</style>
